import 'package:flutter/material.dart';
import 'package:get/get.dart';

import 'util.dart';

class SizeFit {
  static MediaQueryData? _mediaQueryData;
  static double screenWidth = 1;
  static double screenHeight = 1;
  static double navBarHeight = 1;
  static double statusBarHeight = 24;
  static double safeBottom = 0;
  static double rpx = 1;
  static double rh = 1;
  static void initialize() {
    _mediaQueryData = MediaQuery.of(Get.context!);
    screenWidth = _mediaQueryData!.size.width;
    screenHeight = _mediaQueryData!.size.height;
    statusBarHeight = _mediaQueryData!.padding.top;
    navBarHeight = statusBarHeight + 56;
    safeBottom = _mediaQueryData!.padding.bottom;
    rpx = screenWidth / 375;
    rh = screenHeight / 667;
  }

  static EdgeInsets get viewInset {
    return _mediaQueryData!.viewInsets;
  }

  static double setRpx(double size) {
    return rpx * size;
  }

  ///value: 文本内容；fontSize : 文字的大小；fontWeight：文字权重；maxWidth：文本框的最大宽度；maxLines：文本支持最大多少行 ；locale：当前手机语言；textScaleFactor：手机系统可以设置字体大小（默认1.0）
  static double calculateTextHeight({
    required String value,
    required double fontSize,
    FontWeight fontWeight = FontWeight.normal,
    required double maxWidth,
    int maxLines = 10,
  }) {
    // value = filterText(value);
    TextPainter painter = TextPainter(

        ///AUTO：华为手机如果不指定locale的时候，该方法算出来的文字高度是比系统计算偏小的。
        locale: WidgetsBinding.instance!.window.locale,
        maxLines: maxLines,
        textDirection: TextDirection.ltr,
        textScaleFactor: MediaQuery.of(Get.context!).textScaleFactor, //字体缩放大小
        text: TextSpan(
            text: value,
            style: TextStyle(
              fontWeight: fontWeight,
              fontSize: fontSize,
            )));
    painter.layout(maxWidth: maxWidth);

    ///文字的宽度:painter.width
    return painter.height;
  }
}

extension DoubleEx on double {
  double get adapter {
    return SizeFit.rpx * this;
  }

  double get rh {
    return SizeFit.rh * this;
  }
}

extension IntEx on int {
  double get adapter {
    return SizeFit.rpx * this;
  }

  double get rh {
    return SizeFit.rh * this;
  }
}

double font10 = 10.sp;
double font11 = 11.sp;
double font12 = 12.sp;
double font13 = 13.sp;
double font14 = 14.sp;
double font15 = 15.sp;
double font16 = 16.sp;
double font17 = 17.sp;
double font18 = 18.sp;
double font19 = 19.sp;
double font20 = 20.sp;

double buttonHeight = 44.r;

double gap12 = 12.r;
double gap10 = 10.r;
double gap15 = 15.r;
double gap20 = 20.r;

double circle4 = 4.r;

Widget separatorLine({
  Color? color,
  double? indent,
  double? endIndent,
}) =>
    Container(
      height: 1.r,
      color: ColorsUtil.separator,
      margin: EdgeInsets.only(left: indent ?? 0, right: endIndent ?? 0),
    );
